home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr26 / netprog.zip / NETPROG.TAR / lock / lockmain.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  828b  |  36 lines

  1. #define    SEQFILE    "seqno"        /* filename */
  2. #define    MAXBUFF       100
  3.  
  4. main()
  5. {
  6.     int    fd, i, n, pid, seqno;
  7.     char    buff[MAXBUFF + 1];
  8.  
  9.     pid = getpid();
  10.     if ( (fd = open(SEQFILE, 2)) < 0)
  11.         err_sys("can't open %s", SEQFILE);
  12.  
  13.     for (i = 0; i < 20; i++) {
  14.         my_lock(fd);            /* lock the file */
  15.  
  16.         lseek(fd, 0L, 0);        /* rewind before read */
  17.         if ( (n = read(fd, buff, MAXBUFF)) <= 0)
  18.             err_sys("read error");
  19.         buff[n] = '\0';        /* null terminate for sscanf */
  20.  
  21.         if ( (n = sscanf(buff, "%d\n", &seqno)) != 1)
  22.             err_sys("sscanf error");
  23.         printf("pid = %d, seq# = %d\n", pid, seqno);
  24.  
  25.         seqno++;        /* increment the sequence number */
  26.  
  27.         sprintf(buff, "%03d\n", seqno);
  28.         n = strlen(buff);
  29.         lseek(fd, 0L, 0);        /* rewind before write */
  30.         if (write(fd, buff, n) != n)
  31.             err_sys("write error");
  32.  
  33.         my_unlock(fd);            /* unlock the file */
  34.     }
  35. }
  36.